package com.zcx.qshop.conn;

import org.json.JSONArray;
import org.json.JSONObject;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.note.HttpInlet;
import com.zcx.qshop.entity.Good;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@HttpInlet(Conn.INLET_JSON_ORDERSUBMIT)
public class JsonOrdersubmitAsyGet extends QSAsyGet<JsonOrdersubmitAsyGet.Info> {

	public String uid, action, shopcarid, pid, gattributeid, number;

	public JsonOrdersubmitAsyGet(String uid, String action, String shopcarid, String pid, String gattributeid, String number, AsyCallBack<Info> asyCallBack) {

		super(asyCallBack);
		
		this.uid = uid;

		this.action = action;

		this.shopcarid = shopcarid;

		this.pid = pid;

		this.gattributeid = gattributeid;

		this.number = number;
		
 	} 
	
	@Override
	public Info parser(JSONObject object) {
		
		if(object.optString("message").equals("1")){

			Info info = new Info();

			info.name = object.optString("name");

			info.mobile = object.optString("mobile");

			info.address = object.optString("address");

			info.totalprice = object.optString("totalprice");

			info.yunfei = object.optString("yunfei");

			info.count = object.optString("count");

			info.shopcarid = object.optString("shopcarid");

			info.action = object.optString("action");

			info.gattributeid = object.optString("gattributeid");

			info.pid = object.optString("pid");

			info.number = object.optString("number");

			if(info.action.equals("shopcar")){

				info.number = info.action;

			}

			JSONArray array = object.optJSONArray("goodslist");

			if(array != null){

				for(int i = 0 ; i < array.length() ; i ++){

					Good good = new Good();

					JSONObject o = array.optJSONObject(i);

					good.title = o.optString("title");

					good.picurl = Conn.SERVICE + o.optString("picurl");

					good.unit = o.optString("unit");

					good.number = o.optString("number");

					good.price = o.optString("price");

					good.one = o.optString("one");

					good.two = o.optString("two");

					info.goods.add(good);

				}

			}

			JSONObject o = object.optJSONObject("goodslist");

			if(o != null){

				Good good = new Good();

				good.title = o.optString("title");

				good.picurl = Conn.SERVICE + o.optString("picurl");

				good.unit = o.optString("unit");

				good.number = o.optString("number");

				good.price = o.optString("price");

				good.one = o.optString("one");

				good.two = o.optString("two");

				info.goods.add(good);

			}

			TOAST = "订单创建成功";
			
			return info;
			
		}else if(object.optString("message").equals("2")){

			TOAST = "库存不足";

			return null;

		}

		TOAST = "订单创建失败";
		
		return null;
		
	}

	public static class Info implements Serializable{

		public String name, mobile, address, totalprice, yunfei, count, shopcarid, action, gattributeid, pid, number;

		public List<Good> goods = new ArrayList<>();

	}

}
